package com.test.hello.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mysql.fabric.xmlrpc.base.Array;
import com.test.hello.basic.BasicDAO;
import com.test.hello.dao.UserMapper;
import com.test.hello.dao.UserRoleMapper;
import com.test.hello.model.Role;
import com.test.hello.model.User;
import com.test.hello.model.UserRole;

@Service
public class UserService implements BasicDAO<User> {
	@Autowired
	private UserMapper mapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private RoleService roleService;
	public int deleteByPrimaryKey(Long id) {
		
		return mapper.deleteByPrimaryKey(id);
	}

	public int insertSelective(User entity) {
		
		return mapper.insert(entity);
	}

	public User selectByPrimaryKey(Long id) {
		
		return mapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKeySelective(User entity) {
		
		return mapper.updateByPrimaryKeySelective(entity);
	}
	
	
	public User findByName(String username){
		/*User user=mapper.findByName(username);
		List<UserRole> userRoles=userRoleMapper.findByUserid(user.getId());
		user.setRoleList(userRoles);
		return ;*/
		User user=mapper.findByName(username);
		if(user!=null){
			List<Role> roles=new ArrayList<Role>();
			for (Role role : user.getRoleList()) {
				roles.add(roleService.findById(role.getId()));
			}
			user.setRoleList(roles);
		}
		System.out.println(user);
		return user;
	}

}
